package com.junsi.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Newrecord;
import com.junsi.bean.Staff;
import com.junsi.service.NewrecordService;
import com.junsi.util.InteractiveUtil;

@Controller
@RequestMapping("/newrecord")
public class NewrecordController {
	
	@Autowired
	private NewrecordService newrecordService;
	
	/**
	 * 新增
	 * @param newrecord
	 * @param request
	 * @param response
	 */
	@RequestMapping("/saveNewrecord")
	public void saveNewrecord(Newrecord newrecord,HttpServletRequest request,HttpServletResponse response){
		Staff staff = (Staff)request.getSession().getAttribute("staff");
		newrecord.setNewrecord_staffName(staff.getStaff_name());
		boolean b = newrecordService.save(newrecord);
		if(b){
			InteractiveUtil.WriteStringForHtml("新增成功", response);
		}else{
			InteractiveUtil.WriteStringForHtml("新增失败", response);
		}
		
	}
	
	/**
	 * 修改
	 * @param newrecord
	 * @param response
	 */
	@RequestMapping("/updateNewrecord")
	public void updateNewrecord(Newrecord newrecord,HttpServletResponse response){
		boolean b = newrecordService.update(newrecord);
		if(b){
			InteractiveUtil.WriteStringForHtml("修改成功", response);
		}else{
			InteractiveUtil.WriteStringForHtml("修改失败", response);
		}
		
	}
	
	/**
	 * 删除
	 * @param id
	 * @param response
	 */
	@RequestMapping("/deleteNewrecord")
	public void deleteNewrecord(Integer id,HttpServletResponse response){
		boolean b = newrecordService.delete(id);
		if(b){
			InteractiveUtil.WriteStringForHtml("删除成功", response);
		}else{
			InteractiveUtil.WriteStringForHtml("删除失败", response);
		}
		
	}
	
	/**
	 * 根据客户ID查询
	 * @param clientId
	 * @return
	 */
	@RequestMapping("/findByClientId")
	public ModelAndView findByAll(Integer clientId){
		ModelAndView modelAndView = new ModelAndView();
		List<Newrecord> newrecords = newrecordService.findByClientId(clientId);
		modelAndView.addObject("newrecords", newrecords);
		return modelAndView;
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @param response
	 */
	@RequestMapping("/findById")
	public void findById(Integer id,HttpServletResponse response){
		
		Newrecord newrecord = newrecordService.findById(id);
		InteractiveUtil.WriteBeanForHtml(newrecord, response);
		
	}
	
}
